package com.ymhhh.calculatetax;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.method.DigitsKeyListener;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class CalculateTaxActivity extends Activity {

	private Button btnCalculate = null, btnClear = null, btnExit = null,
			btnAboutMe = null;
	private EditText etIncome = null, etValueAdded = null;
	private TextView tvResult = null, tvValueAdd = null, tvIncomeType = null;

	private Spinner spCalType = null, spTaxType = null;
	private ArrayAdapter<?> adapterTaxType = null, adapterCalType = null;

	private CalculateTax ct = null;
	private Double doubleIncome, doubleSocialInsurance;
	private String strPrivateTax;
	private int intCalType, intTaxType = 1;
	private int intYear;

	private TableRow trValueAdd, trCalType;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		InitViews();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	public void InitViews() {
		btnCalculate = (Button) this.findViewById(R.id.btn_main_calok);
		btnClear = (Button) this.findViewById(R.id.btn_main_clear);
		btnExit = (Button) this.findViewById(R.id.btn_main_exit);
		btnAboutMe = (Button) this.findViewById(R.id.btn_about_me);

		etIncome = (EditText) this.findViewById(R.id.et_main_income);
		etValueAdded = (EditText) this.findViewById(R.id.et_main_valueadded);
		spCalType = (Spinner) this.findViewById(R.id.sp_main_caltype);
		spTaxType = (Spinner) this.findViewById(R.id.sp_main_taxtype);
		tvValueAdd = (TextView) this.findViewById(R.id.tv_main_valueadded);
		tvResult = (TextView) this.findViewById(R.id.tv_main_result);
		tvIncomeType = (TextView) this.findViewById(R.id.tv_main_income_type);

		trValueAdd = (TableRow) this
				.findViewById(R.id.tr_main_value_valueadded);
		trCalType = (TableRow) this.findViewById(R.id.tr_main_caltype);

		adapterCalType = ArrayAdapter.createFromResource(this,
				R.array.cal_type, android.R.layout.simple_spinner_item);
		spCalType.setAdapter(adapterCalType);

		adapterTaxType = ArrayAdapter.createFromResource(this,
				R.array.tax_type, android.R.layout.simple_spinner_item);
		spTaxType.setAdapter(adapterTaxType);

		spCalType.setOnItemSelectedListener(spCalTypeSelect);
		spTaxType.setOnItemSelectedListener(spTaxTypeSelect);

		btnCalculate.setOnClickListener(calculateTaxListener);
		btnClear.setOnClickListener(clearListener);
		btnExit.setOnClickListener(exitListener);
		btnAboutMe.setOnClickListener(aboutMeListener);
		ct = new CalculateTax();
	}

	public final Spinner.OnItemSelectedListener spCalTypeSelect = new Spinner.OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			intCalType = arg2 + 1;
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
		}

	};

	public final Spinner.OnItemSelectedListener spTaxTypeSelect = new Spinner.OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			tvResult.setText("");
			intTaxType = arg2 + 2;

			if (intTaxType > 2) {
				if (intTaxType == 3 || intTaxType == 4) {
					tvIncomeType.setText(R.string.main_income_average_month);
					trCalType.setVisibility(View.INVISIBLE);
					tvValueAdd.setText(R.string.main_valueadded_2);
					etValueAdded.setKeyListener(DigitsKeyListener
							.getInstance("0123456789"));
					etIncome.setHint(R.string.main_hint_income_month);
					etValueAdded.setHint(R.string.main_hint_year);
				} else {
					etIncome.setHint(R.string.main_hint_income_one);
					tvIncomeType.setText(R.string.main_income_one);
					trValueAdd.setVisibility(View.INVISIBLE);
					trCalType.setVisibility(View.INVISIBLE);
				}
			} else {
				tvIncomeType.setText(R.string.main_income_month);
				trValueAdd.setVisibility(View.VISIBLE);
				trCalType.setVisibility(View.VISIBLE);
				tvValueAdd.setText(R.string.main_valueadded_1);
				etValueAdded.setKeyListener(DigitsKeyListener
						.getInstance("0123456789."));
				etIncome.setHint(R.string.main_hint_income_month);
				etValueAdded.setHint(R.string.main_hint_social_insurance);
			}

		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
		}

	};

	public final Button.OnClickListener clearListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Clear blanks
			etIncome.setText("");
			etValueAdded.setText("");
		}
	};

	public final Button.OnClickListener exitListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			CalculateTaxActivity.this.finish();
		}
	};

	public final Button.OnClickListener aboutMeListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			final TextView message = new TextView(CalculateTaxActivity.this);
			final SpannableString s = new SpannableString(
					CalculateTaxActivity.this.getText(R.string.about_me_info));
			Linkify.addLinks(s, Linkify.EMAIL_ADDRESSES);
			message.setText(s);
			message.setMovementMethod(LinkMovementMethod.getInstance());
			AlertDialog.Builder builder = new AlertDialog.Builder(
					CalculateTaxActivity.this);
			builder.setView(message).setTitle(
					CalculateTaxActivity.this.getText(R.string.about_me));

			builder.create().show();
		}
	};

	public final Button.OnClickListener calculateTaxListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Calculate
			tvResult.setText("");
			try {
				doubleIncome = Double.parseDouble(etIncome.getText().toString()
						.trim());
			} catch (Exception e) {
				ToastString(CalculateTaxActivity.this
						.getString(R.string.err_input_income));
				return;
			}

			try {
				if (intTaxType < 3) {
					try {
						doubleSocialInsurance = Double.parseDouble(etValueAdded
								.getText().toString());
					} catch (Exception e) {
						ToastString(CalculateTaxActivity.this
								.getString(R.string.err_input_insurance));
						return;
					}
					strPrivateTax = CalculateTaxActivity.this
							.getString(R.string.toast_month_tax)
							+ ct.CalculateType(intCalType, doubleIncome,
									doubleSocialInsurance, 0);
				} else if (intTaxType == 3 || intTaxType == 4) {
					try {
						intYear = Integer.parseInt(etValueAdded.getText()
								.toString());
					} catch (Exception e) {
						ToastString(CalculateTaxActivity.this
								.getString(R.string.err_input_year));
						return;
					}
					strPrivateTax = intYear
							+ CalculateTaxActivity.this
									.getString(R.string.toast_year_tax)
							+ ct.CalculateType(intTaxType, doubleIncome, 0,
									intYear);
				} else {
					strPrivateTax = CalculateTaxActivity.this
							.getString(R.string.toast_private_tax)
							+ ct.CalculateType(intTaxType, doubleIncome, 0, 0);
				}

				tvResult.setText(strPrivateTax);
			} catch (Exception e) {
				return;
			}
		}

	};

	private void ToastString(String text) {
		Toast.makeText(CalculateTaxActivity.this, text, Toast.LENGTH_SHORT)
				.show();
	}

}